﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negocio.Cabelo;
using Negocio.Pele;

namespace qbaproject.Pele.Cor.Consultar
{
    public partial class consultarCorPele : System.Web.UI.Page
    {
        private int paginaAtual = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnk_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pele/Cor/Incluir/incluirCorPele.aspx");
        }

        protected void lkbPesquisar_Click(object sender, EventArgs e)
        {
            carregarGrid();
        }

        protected void grdResCorPele_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            paginaAtual = e.NewPageIndex;
            carregarGrid();
        }

        protected void grdResCorPele_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "editar":
                    var idCorCabelo = e.CommandArgument.ToString();
                    Response.Redirect("~/Pele/Cor/Editar/editarCorPele.aspx?id=" + idCorCabelo);
                    break;
                case "remover":
                    var idCorPeleRemover = e.CommandArgument.ToString();
                    hdfIdCorPeleRemover.Value = idCorPeleRemover;
                    ModalPopupRemover.Show();
                    break;
            }
        }

        protected void lkbSim_Click(object sender, EventArgs e)
        {
            var idCorPele = Convert.ToInt32(hdfIdCorPeleRemover.Value);
            var obNCorPele = new NCorPele();
            obNCorPele.Remover(idCorPele);
            carregarGrid();
        }

        protected void lkbNao_Click(object sender, EventArgs e)
        {
            ModalPopupRemover.Hide();
        }

        private void carregarGrid()
        {
            var obNCorPele = new NCorPele();
            var obListCorPele = obNCorPele.pegarCoresPele(txtNmCorPele.Text);

            if (obListCorPele.Tables[0].Rows.Count != 0)
            {
                divMsnPesquisaSucesso.Visible = true;
                lblNumEncontrados.Text = "Foram encontrados " + obListCorPele.Tables[0].Rows.Count.ToString() + " registros.";
                divMsnPesquisaAtencao.Visible = false;
            }
            else
            {
                divMsnPesquisaSucesso.Visible = false;
                divMsnPesquisaAtencao.Visible = true;
            }

            grdResCorPele.PageSize = 15;
            grdResCorPele.AllowPaging = true;
            grdResCorPele.PageIndex = paginaAtual;
            grdResCorPele.DataSource = obListCorPele;
            grdResCorPele.DataBind();
        }
    }
}